﻿//using Newtonsoft.Json.Linq;

namespace KingerRobot.MexicoSailunMDC.Biz;
/// <summary>
/// 出库扫描后一段线体 做出库数据分析用
/// </summary>
public class OutScannerAfterBiz : K8.MDCBB.Biz
{
    private const string ClassName = nameof(LineScannerBiz);
    //{"LineScannerName":"出库扫描1号","LineScannerLoc":20140101}
    #region 配置参数
    private ParamData Param { get; set; }

    class ParamData
    {
        /// <summary>
        /// 扫描仪位置ID
        /// </summary>
        public long LineScannerLoc { get; set; }
        /// <summary>
        /// 扫描仪名称
        /// </summary>
        public string LineScannerName { get; set; }
    }
    #endregion

    #region PLC 通讯参数
    private ValueData Data { get; set; }
    class ValueData
    {
        /// <summary>
        /// 产品条码
        /// </summary>
        public string ProductCode { get; set; }

    }
    #endregion

    #region 缓存
    private CacheData Cache { get; set; }
    class CacheData
    {
        /// <summary>
        /// 计数
        /// </summary>
        public long TireCount { get; set; }
        /// <summary>
        /// 条码
        /// </summary>
        public string ProductCode { get; set; }
        /// <summary>
        /// 扫描条码 更新后以此为准
        /// </summary>
        //public string ScannerProduct { get; set; }
    }
    #endregion

    #region 实时日志
    private StepData Step { get; set; }
    class StepData
    {
        private string _msg = string.Empty;
        public string StepMsg
        {
            get
            {
                return this._msg;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    this._msg = string.Empty;
                    return;
                }
                this._msg = $"{ClassName}|{System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}>>{value}";
            }
        }
    }
    private void StepMsg(string msg)
    {
        this.Step.StepMsg = msg;
        WriteLog(msg);
    }
    #endregion

    #region 文件日志（运行需要 Nlog 配置）
    private void WriteLog(string message)
    {
        K8.LSF.Client.Log(new K8.LSF.Log { Data = message });
    }
    #endregion 

    /// <summary>
    /// 和 PLC 通讯对象
    /// </summary>
    private BizData BizData { get; set; }
    public int Execute(BizData writer)
    {
        // Excute 方法外使用 BizData
        this.BizData = writer;
        // 返回参数
        var result = 0;
        //this.StepMsg($"扫描{Value.ProductCode}——开始");
        // 获取条码
        var productCode = Data.ProductCode;
        if (String.IsNullOrWhiteSpace(productCode))
        {
            this.StepMsg($"条码不存在");
            return result;
        }
        if (productCode.Contains(ConstFields.Default) || productCode.Contains(ConstFields.NoRead))
        {
            return result;
        }
        // 创建扫码记录
        if (Cache.ProductCode == productCode)
        {
            this.StepMsg($"条码{Data.ProductCode}已添加");
            return result;
        }
        var lineScannerLoc = Param.LineScannerLoc;
        var scannerService = K8.RSF.Client.Create<IScannerService>();
        var scannerResult = scannerService.AddWcsOutScannerAfter(lineScannerLoc, productCode);
        if (scannerResult > 0)
        {
            Cache.ProductCode = productCode;
        }
        return result;
    }

}
